﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using Caliburn.Micro;
using OsmerFvg.Resources;

namespace OsmerFvg.ViewModels
{
    public class RadarPageViewModel : Screen
    {

        public enum Images
        {
            Riflettivita,
            Precipitazione,
            PrecipitazioneIn6Ore,
            Animazione
        }

        private readonly INavigationService _navigationService;

        public RadarPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            DownloadImages();

        }


        private void DownloadImages()
        {
            DownloadImage(Images.Riflettivita);
            DownloadImage(Images.Precipitazione);
            DownloadImage(Images.PrecipitazioneIn6Ore);
            DownloadImage(Images.Animazione);
        }

        private async void DownloadImage(Images image)
        {

            string urlString = "";

            switch (image)
            {
                case Images.Riflettivita:
                    urlString = "http://meteo-fvg.mobi/RAD/radar.php?g=VMI_100&p=indexRAD.html";
                    break;
                case Images.Precipitazione:
                    urlString = "http://meteo-fvg.mobi/RAD/radar.php?g=SRI&p=indexRAD.html";
                    break;
                case Images.PrecipitazioneIn6Ore:
                    urlString = "http://meteo-fvg.mobi/RAD/radar.php?g=SRT6h&p=indexRAD.html";
                    break;
                case Images.Animazione:
                    urlString = "http://meteo-fvg.mobi/RAD/radar.php?g=MovieVmi&p=indexRAD.html";
                    break;
            }

            bool imageAvailable;

            var request = (HttpWebRequest)WebRequest.Create(urlString);

            using (var response = await request.GetResponseAsync())
            using (var source = response.GetResponseStream())
            {
                var streamReader = new StreamReader(source, Encoding.UTF8);
                var sWebPage = streamReader.ReadToEnd();
                streamReader.Close();
                imageAvailable = !sWebPage.Contains("[ n. d. ]");
            }

            switch (image)
            {
                case Images.Riflettivita:
                    if (imageAvailable)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { RiflettivitaImageSource = new Uri(AppResources.RiflettivitaImageSource, UriKind.Absolute); });
                        Deployment.Current.Dispatcher.BeginInvoke(() => { ScalaZImageSource = new Uri(AppResources.ScalaZImageSource, UriKind.Absolute); });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { RiflettivitaImageSource = new Uri("/OsmerFvg;component/Images/NoImageAvailable.gif", UriKind.Relative); });
                    }
                    break;
                case Images.Precipitazione:
                    if (imageAvailable)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { PrecipitazioneImageSource = new Uri(AppResources.PrecipitazioneImageSource, UriKind.Absolute); });
                        Deployment.Current.Dispatcher.BeginInvoke(() => { ScalaRImageSource = new Uri(AppResources.ScalaRImageSource, UriKind.Absolute); });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { PrecipitazioneImageSource = new Uri("/OsmerFvg;component/Images/NoImageAvailable.gif", UriKind.Absolute); });
                    }
                    break;
                case Images.PrecipitazioneIn6Ore:
                    if (imageAvailable)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { PrecipitazioneIn6OreImageSource = new Uri(AppResources.PrecipitazioneIn6OreImageSource, UriKind.Absolute); });
                        Deployment.Current.Dispatcher.BeginInvoke(() => { ScalaRtImageSource = new Uri(AppResources.ScalaRtImageSource, UriKind.Absolute); });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { PrecipitazioneIn6OreImageSource = new Uri("/OsmerFvg;component/Images/NoImageAvailable.gif", UriKind.Absolute); });
                        Deployment.Current.Dispatcher.BeginInvoke(() => { ScalaZImageSource = new Uri(AppResources.ScalaZImageSource, UriKind.Absolute); });
                    }
                    break;
                case Images.Animazione:
                    if (imageAvailable)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { AnimazioneImageSource = new Uri(AppResources.AnimazioneImageSource, UriKind.Absolute); });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => { AnimazioneImageSource = new Uri("/OsmerFvg;component/Images/NoImageAvailable.gif", UriKind.Absolute); });
                    }
                    break;
            }




        }



        private Uri _riflettivitaImageSource;
        public Uri RiflettivitaImageSource
        {
            get { return _riflettivitaImageSource; }
            set
            {
                if (_riflettivitaImageSource != value)
                {
                    _riflettivitaImageSource = value;
                    NotifyOfPropertyChange(() => RiflettivitaImageSource);
                }
            }
        }

        private Uri _precipitazioneImageSource;
        public Uri PrecipitazioneImageSource
        {
            get { return _precipitazioneImageSource; }
            set
            {
                if (_precipitazioneImageSource != value)
                {
                    _precipitazioneImageSource = value;
                    NotifyOfPropertyChange(() => PrecipitazioneImageSource);
                }
            }
        }

        private Uri _precipitazioneIn6OreImageSource;
        public Uri PrecipitazioneIn6OreImageSource
        {
            get { return _precipitazioneIn6OreImageSource; }
            set
            {
                if (_precipitazioneIn6OreImageSource != value)
                {
                    _precipitazioneIn6OreImageSource = value;
                    NotifyOfPropertyChange(() => PrecipitazioneIn6OreImageSource);
                }
            }
        }

        private Uri _animazioneImageSource;
        public Uri AnimazioneImageSource
        {
            get { return _animazioneImageSource; }
            set
            {
                if (_animazioneImageSource != value)
                {
                    _animazioneImageSource = value;
                    NotifyOfPropertyChange(() => AnimazioneImageSource);
                }
            }
        }


        private Uri _scalaZImageSource;
        public Uri ScalaZImageSource
        {
            get { return _scalaZImageSource; }
            set
            {
                if (_scalaZImageSource != value)
                {
                    _scalaZImageSource = value;
                    NotifyOfPropertyChange(() => ScalaZImageSource);
                }
            }
        }

        private Uri _scalaRImageSource;
        public Uri ScalaRImageSource
        {
            get { return _scalaRImageSource; }
            set
            {
                if (_scalaRImageSource != value)
                {
                    _scalaRImageSource = value;
                    NotifyOfPropertyChange(() => ScalaRImageSource);
                }
            }
        }

        private Uri _scalaRtImageSource;
        public Uri ScalaRtImageSource
        {
            get { return _scalaRtImageSource; }
            set
            {
                if (_scalaRtImageSource != value)
                {
                    _scalaRtImageSource = value;
                    NotifyOfPropertyChange(() => ScalaRtImageSource);
                }
            }
        }

        public void RiflettivitaTapCommand()
        {
            _navigationService.UriFor<RadarZoomViewModel>()
                .WithParam(x => x.PageHeaderText, "Riflettività")
                .WithParam(x => x.FirstImageSource, RiflettivitaImageSource)
                .WithParam(x => x.SecondImageSource, ScalaZImageSource)
                .Navigate();
        }

        public void PrecipitazioneTapCommand()
        {
            _navigationService.UriFor<RadarZoomViewModel>()
                .WithParam(x => x.PageHeaderText, "Precipitazione (intensità)")
                .WithParam(x => x.FirstImageSource, PrecipitazioneImageSource)
                .WithParam(x => x.SecondImageSource, ScalaRImageSource)
                .Navigate();
        }

        public void PrecipitazioneIn6OreTapCommand()
        {
            _navigationService.UriFor<RadarZoomViewModel>()
                .WithParam(x => x.PageHeaderText, "Precipitazione in 6 Ore")
                .WithParam(x => x.FirstImageSource, PrecipitazioneIn6OreImageSource)
                .WithParam(x => x.SecondImageSource, ScalaRtImageSource)
                .Navigate();
        }

        public void AnimazioneTapCommand()
        {
            _navigationService.UriFor<RadarZoomViewModel>()
                .WithParam(x => x.PageHeaderText, "Animazione")
                .WithParam(x => x.FirstImageSource, AnimazioneImageSource)
                .WithParam(x => x.SecondImageSource, ScalaZImageSource)
                .Navigate();
        }

    }
}
